<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Staff extends BaseModel
{
	protected $table = 'staffs';
    protected $fillable = [
    	'store_id',
	    'staff_no',
	    'department_id',
	    'picture',
	    'register_steel',
	    'id_card',
	    'health_card',
	    'job_card',
	    'salary',
	    'status'
    ];
    
    public function department()
    {
    	return $this->belongsTo(Department::class);
    }
    
    public function store()
    {
    	return $this->belongsTo(Store::class)->with(['detail']);
    }
    
    // 生成工号
    static public function createStaffNo($id)
    {
    	//10000
		$ini = 10000;
		$staff_no = $ini + $id;
		//$staff_no =  substr((string)$str, 1);
		return (int)$staff_no;
    }
    
    public function setSalaryAttribute($value)
    {
        $this->attributes['salary'] = $value * 100;
    }
    
    public function getSalaryAttribute($value)
    {
        return $value / 100;
    }
    // 修改器
	public function setPictureAttribute($value)
	{
		foreach ($value as $k => $v) {
			$value[$k] = Env('ALIOSS_HOST').'/'.$v;
		}
		$this->attributes['picture'] = collect($value)->toJson();
	}
	
	public function setRegisterSteelAttribute($value)
	{
		foreach ($value as $k => $v) {
			$value[$k] = Env('ALIOSS_HOST').'/'.$v;
		}
		$this->attributes['register_steel'] = collect($value)->toJson();
	}
	
	public function setIdCardAttribute($value)
	{
		foreach ($value as $k => $v) {
			$value[$k] = Env('ALIOSS_HOST').'/'.$v;
		}
		$this->attributes['id_card'] = collect($value)->toJson();
	}
	
	public function setHealthCardAttribute($value)
	{
		foreach ($value as $k => $v) {
			$value[$k] = Env('ALIOSS_HOST').'/'.$v;
		}
		$this->attributes['health_card'] = collect($value)->toJson();
	}
	
	public function setJobCardAttribute($value)
	{
		foreach ($value as $k => $v) {
			$value[$k] = Env('ALIOSS_HOST').'/'.$v;
		}
		$this->attributes['job_card'] = collect($value)->toJson();
	}
    /***获取器***/
    public function getPictureAttribute($value)
    {
    	$value = json_decode($value);
    	return $value;
    }
    
    public function getRegisterSteelAttribute($value)
    {
    	$value = json_decode($value);
    	return $value;
    }
    
    public function getIdCardAttribute($value)
    {
    	$value = json_decode($value);
    	return $value;
    }
    
    public function getHealthCardAttribute($value)
    {
    	$value = json_decode($value);
    	return $value;
    }
    
    public function getJobCardAttribute($value)
    {
    	$value = json_decode($value);
    	return $value;
    }
}
